package cn.tx.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.tx.dao.ProductTypeDao;
import cn.tx.model.ProductType;
import cn.tx.query.ProductTypeQuery;

public class ProductTypeDaoImpl extends BaseDaoImpl<ProductType, ProductTypeQuery> implements ProductTypeDao {

	@Override
	public String createHql(ProductTypeQuery q) {
		String hql = "from ProductType e where 1=1 ";
		String condition = this.createConditionHql(q);
		hql = hql + condition + "order by e.productTypeId desc";
		return hql;
	}

	@Override
	public String createHqlCount(ProductTypeQuery q) {
		String hql = "select count(e) from ProductType e where 1=1 ";
		//StringUtils.isNotBlank判断当前的字符串是不是空串和null
		String condition = this.createConditionHql(q);
		hql = hql + condition;
		return hql;
	}

	@Override
	public String createConditionHql(ProductTypeQuery q) {
		String hql = "";
		if(StringUtils.isNotBlank(q.getName())){
			hql = hql + "and e.name like :name ";
		}
		if(q.getSupplierId() != null){
			hql = hql + "and e.supplier.supplierId = :supplierId ";
		}
		
		return hql;
	}

	@Override
	public List<ProductType> queryProductTypeBySupName(ProductTypeQuery q) {
		String hql = "from ProductType e where 1=1 and e.name = ? and e.supplier.supplierId = ? ";
		List<ProductType> list = this.getHibernateTemplate().find(hql, new Object[]{q.getName(), q.getSupplier().getSupplierId()});
		
		return list;
	}



}
